package com.education.upload.controller;

import com.education.upload.pojo.MultipartFileParam;
import com.education.upload.service.UploadService;
import com.education.upload.utils.AjaxResult;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author: ly
 * @create: 2018-06-24 17:10
 **/
@RestController
@RequestMapping("upload")
public class UploadController {

    public static final Logger LOGGER = LogManager.getLogger(UploadController.class);

    @Autowired
    private UploadService uploadService;

//    @PostMapping("image")
//    public ResponseEntity<String> uploadImage(@RequestParam("file")MultipartFile file){
//        String url = this.uploadService.uploadImage(file);
//        if(StringUtils.isBlank(url)){
//            // 上传失败，返回400
//            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
//        }
//        // 返回201
//        return new ResponseEntity<>(url, HttpStatus.CREATED);
//    }

    //获取uploadId
    @PostMapping(value = "/getUploadId")
    public AjaxResult getUploadId(@RequestBody MultipartFileParam multipartFileParam) {
        LOGGER.info("/getUploadId {{}}", multipartFileParam.toString());
        try {
            Map<String, Object> map = uploadService.getUploadId(multipartFileParam);
            return AjaxResult.success(map);
        } catch (Exception e) {
            LOGGER.error("getUploadId error", e);
            return AjaxResult.error();
        }
    }

    //调用 ApiUploadV2InitUpload api 创建一个 upload 任务，获取 uploadId
    @PostMapping(value = "/uploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public AjaxResult uploadFile(MultipartFileParam multipartFileParam) {
        try {
            Map<String, Object> map = uploadService.uploadFile(multipartFileParam);
            if (map == null) {
                return AjaxResult.error();
            }
            return AjaxResult.success(map);
        } catch (Exception e) {
            LOGGER.error("uploadFile error", e);
            return AjaxResult.error();
        }
    }

    //上传完毕确认接口
    @PostMapping(value = "/uploadFileOk")
    public AjaxResult uploadFileOk(@RequestBody MultipartFileParam multipartFileParam) {
        try {
            String fileName = uploadService.uploadFileOk(multipartFileParam);
            if (null == fileName) {
                return AjaxResult.error();
            }
            return AjaxResult.success(fileName);
        } catch (Exception e) {
            LOGGER.error("uploadFileOk error", e);
            return AjaxResult.error();
        }
    }


//    @GetMapping(value = "/uploadFileTest")
//    public AjaxResult uploadFileTest() {
//        try {
//            String bool = uploadService.uploadFileTest();
//            return AjaxResult.success(bool);
//        } catch (Exception e) {
//            LOGGER.error("uploadFileOk error", e);
//            return AjaxResult.error();
//        }
//    }
}
